package com.lcf.system.entity;

import com.lcf.framework.model.BaseNoTenantModel;
import com.lcf.system.dto.OrderGoodsDTO;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.util.Map;

/**
 * 订单商品(OrderGoods)表实体类
 *
 * @author lcf
 * @since 2025-05-02 11:55:01
 */
@EqualsAndHashCode(callSuper = true)
@Data
@NoArgsConstructor
public class OrderGoods extends BaseNoTenantModel<OrderGoods> {
    //id
    private Long id;
    //订单id
    private Long orderId;
    //订单编号
    private String orderNo;
    //商品id
    private Long goodsId;
    //商品名称
    private String goodsName;
    //商品数量
    private Integer goodsNum;
    //商品总价
    private BigDecimal goodsPrice;
    //实际总价
    private BigDecimal realPrice;
    //商品封面
    private String goodsImg;

    public OrderGoods(SysOrder sysOrder, OrderGoodsDTO orderGoodsDTO,
                      Map<Long, Goods> goodsMap, RoleDetail detail) {
        this.orderId = sysOrder.getId();
        this.orderNo = sysOrder.getOrderNo();
        this.goodsId = orderGoodsDTO.getGoodsId();
        this.goodsNum = orderGoodsDTO.getGoodsNum();

        Goods goods = goodsMap.get(this.goodsId);
        this.goodsName = goods.getGoodsName();
        this.goodsPrice = BigDecimal.valueOf(this.goodsNum).multiply(goods.getGoodsPrice());
        this.realPrice = detail == null ? this.goodsPrice : BigDecimal.valueOf(this.goodsNum).multiply(detail.getDiscount());
        this.goodsImg = goods.getGoodsImg();
    }
}

